//
//  DHMainViewController.m
//  Sina.Weibo.OC
//
//  Created by Holfioo on 15/9/3.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

#import "DHMainViewController.h"
#import "DHHomeController.h"   
#import "DHMeController.h"
#import "DHMessageController.h"
#import "DHDiscoverController.h"

@interface DHMainViewController ()

@property (nonatomic, strong) UIButton *addBtnView;
@end

@implementation DHMainViewController
// 懒加载tabbar的按钮
- (UIButton *)addBtnView
{
    if (nil == _addBtnView) {
        // 设置当前按钮的属性
        _addBtnView = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtnView setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_addBtnView setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_addBtnView setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_addBtnView setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [self.tabBar addSubview:_addBtnView];
        
        //:
        [_addBtnView addTarget:self action:@selector(addBtnViewClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _addBtnView;
}

// 监听按钮的点击
- (void)addBtnViewClick {
    NSLog(@"%s", __func__);
}

- (void)setbtnFrame {
    // 计算Frame.
    NSInteger count = self.childViewControllers.count;
    CGFloat w = self.tabBar.bounds.size.width / count;
    
    self.addBtnView.frame = CGRectMake(w * 2, 0, w, self.tabBar.bounds.size.height);
}
// 将frame加到生命周期中
- (void)viewWillAppear:(BOOL)animated
{
    // 此时才布局子控件。
    [super viewWillAppear:animated];
    [self setbtnFrame];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)loadView{
    [super loadView];
    // 布局控件。
    [self addChildViewControllerWithVC:[[DHHomeController alloc] init] title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewControllerWithVC:[[DHMessageController alloc] init] title:@"消息" imageName:@"tabbar_message_center"];
    [self addChildViewController:[[UIViewController alloc] init]];
    [self addChildViewControllerWithVC:[DHDiscoverController new] title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewControllerWithVC:[DHMeController new] title:@"我" imageName:@"tabbar_profile"];
}

- (void)addChildViewControllerWithVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)name
{
//    UIViewController *hc = vc;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    // 注意这里设置的时候给hc设置
    self.tabBar.tintColor = [UIColor orangeColor];
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:name];
    [self addChildViewController:nav];
}

@end
